﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using System.Xml;

namespace KeaoMDS.InfoManage
{
    public partial class CheLiangPaiHao : BaseForm
    {
        public CheLiangPaiHao()
        {
            InitializeComponent();

            BindChePaiHao();
            BindZiMu();
            BindShuZi();
            BindNeiBuCheLiang();
        }

        public string m_Current = string.Empty;

        private Dictionary<string, string> m_ShengFen = new Dictionary<string, string>();

        private void CheLiangPaiHao_Load(object sender, EventArgs e)
        {
            m_Current = string.Empty;
            cmb_putongcheliang.Text = string.Empty;
        }

        /// <summary>
        /// 绑定车号归属地
        /// </summary>
        private void BindChePaiHao()
        {
            try
            {
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add("shouzimu", "asc");
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_shengfen", orderBy);
                if (Dt == null)
                {
                    return;
                }

                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    Button btn = new Button();
                    btn.Dock = DockStyle.Fill;
                    btn.Parent = tableLayoutPanel1;
                    btn.Text = string.Format("{0}", Dt.Rows[i]["shengfen"]);
                    btn.Click += new EventHandler(btn_Click);
                    tableLayoutPanel1.Controls.Add(btn);
                    m_ShengFen.Add(string.Format("{0}", Dt.Rows[i]["shengfen"]), string.Format("{0}", Dt.Rows[i]["shouzimu"]));
                    cmb_putongcheliang.Items.Add(string.Format("{0}", Dt.Rows[i]["shengfen"]));
                }

               
            }
            catch (Exception err)
            {
                KeaoUtils.LogTxt(string.Format("绑定车牌号信息异常。#{0}", err.Message));
            }
        }
/// <summary>
/// 点击车号归属地
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            cmb_putongcheliang.Text = btn.Text;
        }

        /// <summary>
        /// 绑定字母按钮
        /// </summary>
        private void BindZiMu()
        {
            byte[] ziMuBytes = new byte[1];
            for (int i = 65; i < 91; i++)
            {
                ziMuBytes[0] =Convert.ToByte(i);
                Button btnZiMu = new Button();
                btnZiMu.Dock = DockStyle.Fill;
                btnZiMu.Parent = tableLayoutPanel2;
                btnZiMu.Text = Encoding.ASCII.GetString(ziMuBytes);
                btnZiMu.Click += new EventHandler(btnZiMu_Click);
            }

        }
        /// <summary>
        /// 点击字母按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnZiMu_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Button btn = sender as Button;
            if (cmb_putongcheliang.Text.Length >= 1)
            {
                cmb_putongcheliang.Text += btn.Text;
            }

        }

        /// <summary>
        /// 绑定数字按钮
        /// </summary>
        private void BindShuZi()
        {
            for (int i = 0; i < 10; i++)
            {
                Button btnShuZi = new Button();
                btnShuZi.Dock = DockStyle.Fill;
                btnShuZi.Parent = tableLayoutPanel3;
                btnShuZi.Text = i.ToString();
                btnShuZi.Click += new EventHandler(btnShuZi_Click);

                Button btnNeiBuShuZi = new Button();
                btnNeiBuShuZi.Dock = DockStyle.Fill;
                btnNeiBuShuZi.Parent = tableLayoutPanel5;
                btnNeiBuShuZi.Text = i.ToString();
                btnNeiBuShuZi.Click += new EventHandler(btnNeiBuShuZi_Click);
            }

            Button btnHengXian = new Button();
            btnHengXian.Dock = DockStyle.Fill;
            btnHengXian.Parent = tableLayoutPanel5;
            btnHengXian.Text = "-";
            btnHengXian.Click += new EventHandler(btnNeiBuShuZi_Click);
        }

        private void BindNeiBuCheLiang()
        {
            XmlNodeList nodeList=KeaoUtils.ConfigGetNodeListByTag("车辆");
            foreach (XmlNode node in nodeList)
            {
                Button btn = new Button();
                btn.Dock = DockStyle.Fill;
                btn.Parent = tableLayoutPanel4;
                btn.Click += new EventHandler(btnNeiBuShuZi_Click);
                btn.Text = node.InnerText;
            }
        }

        /// <summary>
        /// 内部车辆点击按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNeiBuShuZi_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            cmb_putongcheliang.Text += btn.Text;
        }

        /// <summary>
        /// 普通车辆点击数字按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnShuZi_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (cmb_putongcheliang.Text.Length >= 2 && cmb_putongcheliang.Text.Length < 7)
            {
                cmb_putongcheliang.Text += btn.Text;
            }
        }

        private void btn_putong_del_Click(object sender, EventArgs e)
        {
            if (cmb_putongcheliang.Text.Length > 0)
            {
                cmb_putongcheliang.Text = cmb_putongcheliang.Text.Substring(0,cmb_putongcheliang.Text.Length-1);
            }
        }


        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            m_Current = cmb_putongcheliang.Text;
            DialogResult = DialogResult.OK;
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 处理键盘按键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmb_putongcheliang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                menu_btn_baocun_Click(null, null);
                return;
            }
            if (e.KeyChar == 8&&cmb_putongcheliang.Text.Length==1)
            {
                cmb_putongcheliang.Items.Clear();
                foreach (string str in m_ShengFen.Keys)
                {
                    cmb_putongcheliang.Items.Add(str);
                }
                cmb_putongcheliang.Text = string.Empty;
                return;
            }
            if (cmb_putongcheliang.Text.Length == 0)
            {
                cmb_putongcheliang.Items.Clear();
                string inputStr = e.KeyChar.ToString().ToUpper();
                string tmpStr = string.Empty;
                foreach (string str in m_ShengFen.Keys)
                {
                    tmpStr = string.Empty;
                    m_ShengFen.TryGetValue(str, out tmpStr);
                    if (tmpStr == inputStr)
                    {
                        cmb_putongcheliang.Items.Add(str);
                    }
                }
                if (cmb_putongcheliang.Items.Count > 0)
                {
                    cmb_putongcheliang.SelectedIndex = 0;
                    cmb_putongcheliang.DroppedDown = true;
                }
                e.Handled = true;
                return;
            }
            cmb_putongcheliang.SelectionLength = 0;
            cmb_putongcheliang.SelectionStart = cmb_putongcheliang.Text.Length;
            e.KeyChar = e.KeyChar.ToString().ToUpper().ToCharArray(0,1)[0];
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                cmb_putongcheliang.KeyPress += new KeyPressEventHandler(cmb_putongcheliang_KeyPress);
            }
            else
            {
                cmb_putongcheliang.KeyPress -= cmb_putongcheliang_KeyPress;
            }
        }


    }
}
